//
//  BoxSelectViewController.m
//  ForestPack
//
//  Created by 郑洲 on 2018/7/18.
//  Copyright © 2018年 郑洲. All rights reserved.
//

#import "BoxSelectViewController.h"

//#import "MaterialSelectViewController.h"
#import "CoverStyleViewController.h"

#import "BoxSizeTableViewCell.h"
#import "TypeSelectView.h"

@interface BoxSelectViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) BaseTableView     *tableView;
@property (nonatomic, strong) TypeSelectView    *boxTypeView;
@property (nonatomic, strong) NSArray           *dataArray;
@property (nonatomic, strong) UIImageView       *boxImage;
@property (nonatomic, assign) NSInteger         selectType;

@end

@implementation BoxSelectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"箱型选择";
    self.dataArray = [NSArray array];
    
    self.tableView = [[BaseTableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - NAVIGATION_BAR_HEIGHT - ScreenX375(49)) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.tableView];
    
    UIButton *connectBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT - NAVIGATION_BAR_HEIGHT - ScreenX375(49), ScreenX375(258), ScreenX375(49))];
    connectBtn.backgroundColor = REXADECIMALCOLOR(@"#ff9500");
    [connectBtn setTitle:@"有困难请联系我" forState:UIControlStateNormal];
    [connectBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    connectBtn.titleLabel.font = Kfont(14);
    [self.view addSubview:connectBtn];
    
    UIButton *nextBtn = [[UIButton alloc] initWithFrame:CGRectMake(ScreenX375(258), SCREEN_HEIGHT - NAVIGATION_BAR_HEIGHT - ScreenX375(49), SCREEN_WIDTH - ScreenX375(258), ScreenX375(49))];
    nextBtn.backgroundColor = REXADECIMALCOLOR(@"#ed223b");
    [nextBtn setTitle:@"下一步" forState:UIControlStateNormal];
    [nextBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    nextBtn.titleLabel.font = Kfont(14);
    [nextBtn addTarget:self action:@selector(nextStep) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:nextBtn];
    
    [self getData];
}

- (void)nextStep {
    BoxSizeTableViewCell *cell = (BoxSizeTableViewCell *)[_tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
    if (cell.longField.text == 0) {
        [HttpManager showNoteMsg:@"请输入内径长度"];
        return;
    }
    if (cell.widthField.text == 0) {
        [HttpManager showNoteMsg:@"请输入内径宽度"];
        return;
    }
    if (cell.heightField.text == 0) {
        [HttpManager showNoteMsg:@"请输入内径高度"];
        return;
    }
    NSString *boxTypeId = [NSString stringWithFormat:@"%@",self.dataArray[_selectType][@"id"]];
    kNSUDefaultSaveVauleAndKey(boxTypeId, @"BoxTypeId");
    kNSUDefaultSaveVauleAndKey(cell.longField.text, @"BoxSizeLong");
    kNSUDefaultSaveVauleAndKey(cell.widthField.text, @"BoxSizeWidth");
    kNSUDefaultSaveVauleAndKey(cell.heightField.text, @"BoxSizeHeight");
    CoverStyleViewController *vc = [[CoverStyleViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)getData {
    [HttpManager postHttpRequestByPost:[NSString stringWithFormat:@"%@/get_box_model_list",Auto_REQUESTHEADER] andParameter:nil success:^(id successResponse) {
        NSString *status = [NSString stringWithFormat:@"%@",successResponse[@"code"]];
        if ([status isEqualToString:@"0"]) {
            self.dataArray = successResponse[@"data"];
            self.selectType = 0;
            if (kNSUDefaultReadKey(@"BoxTypeId")) {
                for (int i = 0; i < self.dataArray.count; i++) {
                    NSString *boxTypeId = [NSString stringWithFormat:@"%@",self.dataArray[i][@"id"]];
                    if ([boxTypeId isEqualToString:kNSUDefaultReadKey(@"BoxTypeId")]) {
                        self.selectType = i;
                    }
                }
            }
            if (self.boxImage) {
                [self.boxImage sd_setImageWithURL:[NSURL URLWithString:self.dataArray[self.selectType][@"image"]] placeholderImage:nil options:SDWebImageRetryFailed];
            }
            [self setTypeView];
        }else {
            [HttpManager showNoteMsg:[NSString stringWithFormat:@"%@",successResponse[@"msg"]]];
        }
    } andFailure:^(id failureResponse) {
        [HttpManager showFail];
    }];
}

- (void)setTypeView {
    self.boxTypeView = [[TypeSelectView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT)];
    NSMutableArray *noteArr = [NSMutableArray array];
    for (NSDictionary *dic in self.dataArray) {
        [noteArr addObject:dic[@"name"]];
    }
    [self.boxTypeView modelArrayForView:noteArr andSelect:_selectType];
    __weak __typeof(self) weakSelf = self;
    self.boxTypeView.hiddenClick = ^{
        [weakSelf hidden];
    };
    self.boxTypeView.doneClick = ^(NSInteger selectIndex) {
        weakSelf.selectType = selectIndex;
        [weakSelf.boxImage sd_setImageWithURL:[NSURL URLWithString:weakSelf.dataArray[weakSelf.selectType][@"image"]] placeholderImage:nil options:SDWebImageRetryFailed];
        [weakSelf hidden];
    };
    [kKeyWindow addSubview:self.boxTypeView];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        static NSString *cellId = @"ImageCell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
            
            UILabel *noteLabel = [[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(13), ScreenX375(14), ScreenX375(80), ScreenX375(16))];
            noteLabel.text = @"箱型图片：";
            noteLabel.textColor = REXADECIMALCOLOR(@"#333333");
            noteLabel.font = Kfont(14);
            [cell addSubview:noteLabel];
            
            _boxImage = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenX375(86), ScreenX375(15), ScreenX375(79), ScreenX375(79))];
            _boxImage.layer.cornerRadius = ScreenX375(5);
            _boxImage.clipsToBounds = YES;
            if (_dataArray.count > 0) {
                [_boxImage sd_setImageWithURL:[NSURL URLWithString:_dataArray[_selectType][@"image"]] placeholderImage:nil options:SDWebImageRetryFailed];
            }
            [cell addSubview:_boxImage];
            
            UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenX375(110) - 0.5, SCREEN_WIDTH, 0.5)];
            line.backgroundColor = LineColor;
            [cell addSubview:line];
        }
        return cell;
    }else if (indexPath.row == 1) {
        BoxSizeTableViewCell *cell = [BoxSizeTableViewCell cellWithTableView:tableView andIndexPath:indexPath];
        return cell;
    }
    UITableViewCell *cell = [[UITableViewCell alloc] init];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        return ScreenX375(110);
    }
    return ScreenX375(100);
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return ScreenX375(53);
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, ScreenX375(53))];
    view.backgroundColor = [UIColor whiteColor];
    
    UIView *rline = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenX375(15), ScreenX375(3), ScreenX375(23))];
    rline.backgroundColor = ThemeColor;
    [view addSubview:rline];
    
    UILabel *noteLabel = [[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(13), ScreenX375(15), ScreenX375(300), ScreenX375(24))];
    noteLabel.text = @"箱型";
    noteLabel.textColor = REXADECIMALCOLOR(@"#333333");
    noteLabel.font = Kfont(18);
    [view addSubview:noteLabel];
    
    UIButton *selectBtn = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - ScreenX375(39), 0, ScreenX375(39), ScreenX375(53))];
    [selectBtn setImage:[UIImage imageNamed:@"矢量智能对象拷贝2副本2"] forState:UIControlStateNormal];
    [selectBtn addTarget:self action:@selector(selectBoxType) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:selectBtn];
    
    UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(0, ScreenX375(53) - 0.5, SCREEN_WIDTH, 0.5)];
    line.backgroundColor = LineColor;
    [view addSubview:line];
    
    return view;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    [self.view endEditing:YES];
}

- (void)selectBoxType {
    [self.view endEditing:YES];
    [UIView animateWithDuration:0.3 animations:^{
        self.boxTypeView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    } completion:^(BOOL finished) {
        self.boxTypeView.shadowBtn.alpha = 1;
    }];
}

- (void)hidden {
    self.boxTypeView.shadowBtn.alpha = 0;
    [UIView animateWithDuration:0.3 animations:^{
        self.boxTypeView.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT);
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
